package models;

import java.io.File;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;

import play.data.binding.NoBinding;
import play.data.validation.Email;
import play.data.validation.Password;
import play.data.validation.Required;
import play.db.jpa.Blob;
import play.db.jpa.Model;

@Entity
public class User extends Model {

	@NoBinding
	@Column(insertable = false, updatable = false)
	private String dtype;
	
	@Required
	@NotNull
	public String lastNameUser;

	@Required
	@NotNull
	public String firstNameUser;

	@Email
	@Required
	@NotNull
	public String emailUser;

	@Password
	@Required
	@NotNull
	public String passwordUser;

	public Blob avatarPathUser;

	public User(String lastNameUser, String firstNameUser, String emailUser,
			String passwordUser) {
		super();
		this.lastNameUser = lastNameUser;
		this.firstNameUser = firstNameUser;
		this.emailUser = emailUser;
		this.passwordUser = passwordUser;
		this.avatarPathUser = new Blob();
	}

	@Override
	public String toString() {
		return lastNameUser + " " + firstNameUser;
	}

	public static User connect(String emailUser, String passwordUser) {
		return find("byEmailUserAndPasswordUser", emailUser, passwordUser)
				.first();
	}
	
	public static User create(User enseignant) {
		return enseignant.create() ? enseignant : null;
	}

	public String getDtype() {
		return dtype;
	}
	
	public void matchWithOtherUser(User userToCompare){
		if(this.lastNameUser == null)
			this.lastNameUser = userToCompare.lastNameUser ;
		if(this.firstNameUser == null)
			this.firstNameUser = userToCompare.firstNameUser ;
		if(this.passwordUser == null)
			this.passwordUser = userToCompare.passwordUser ;
	}

}
